﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KBC_Quiz_Show.DBConnect;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using KBC_Quiz_Show.Object;

namespace KBC_Quiz_Show.Bussiness
{
    class QuesstionList
    {
        Utils con;
        SqlConnection conn;
        SqlCommand cmd, cmd1;

        private Question[] list;
        private int n;

        public QuesstionList()
        {
            con = new Utils();
            n = 0;
            list = null;
        }

        public Question getQues(int i)
        {
            if (i < n && i >= 0)
                return list[i];
            else
                return null;
        }
        public int getN()
        {
            return n;
        }

        public void addQues(Question q)
        {
            if (q != null)
            {
                Question[] b = new Question[n + 1];
                int i;
                for (i = 0; i < n; i++)
                {
                    b[i] = new Question();
                    b[i] = list[i];
                }
                b[n] = new Question();
                b[n] = q;
                list = new Question[n + 1];
                n++;
                for (i = 0; i < n; i++)
                {
                    list[i] = new Question();
                    list[i] = b[i];
                }
            }
        }

        public void addQues(Question q, int index)
        {
            if (q != null)
            {
                Question[] b = new Question[n + 1];
                for (int i = 0; i < n; i++)
                {
                    b[i] = new Question();
                    b[i] = list[i];
                }
                for (int i = index; i < n + 1; i++)
                {
                    b[i] = b[i + 1];
                    b[index] = q;
                }

                list = new Question[n + 1];
                n++;
                for (int i = 0; i < n; i++)
                {
                    list[i] = new Question();
                    list[i] = b[i];
                }
            }
        }

        public void addQuesIndex(Question q, int index)
        {
            for (int i = index; i < n+1; i++)
            {
                list[i] = list[i + 1];
                list[index] = q;
            }
        }
        public void removeQues(int index)
        {
            if (index >= 0 && index < n)
            {
                for (int i = 0; i < n - 1; i++)
                    list[i] = list[i + 1];
                n--;
            }
        }

        public void getQuesByLevel(int level)
        {
            SqlDataReader reader1 = null;
            SqlConnection conn1 = con.getConnection();
            SqlConnection conn2 = con.getConnection();
            cmd = new SqlCommand("getCountQuesByLevel", conn1);
            cmd1 = new SqlCommand("getQuesByLevel", conn2);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@level", SqlDbType.Int).Value = level;
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.Add("@level", SqlDbType.Int).Value = level;
            try
            {
                conn1.Open();
                conn2.Open();
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        this.n = reader.GetInt32(0);
                        list = new Question[n];
                        using (reader1 = cmd1.ExecuteReader())
                        {
                            int i = 0;
                            while (reader1.Read())
                            {
                                list[i] = new Question();
                                list[i].QuesID = reader1.GetInt32(0);
                                list[i].QuesContent = reader1.GetString(1);
                                list[i].Ans1 = reader1.GetString(2);
                                list[i].Ans2 = reader1.GetString(3);
                                list[i].Ans3 = reader1.GetString(4);
                                list[i].Ans4 = reader1.GetString(5);
                                list[i].Level = reader1.GetInt32(6);
                                list[i].Anstrue = reader1.GetString(7);
                                
                                i++;
                            }
                        }
                    }
                }
            }
            finally
            {
                conn1.Close();
                conn2.Close();
            }
        }
    }
}
